<?php

namespace App\Api\Controllers;

use \App\Http\Controllers\Controller as BaseController;
use Illuminate\Support\Facades\Redis;

class Controller extends BaseController
{

    public $kingdeeUrl = 'http://121.36.11.125';

    public function jsonResponse(array $data = [],$code = []) {
        $result = [
            'code' => $code,
            'message' => 'success',
            'data' => $data,
        ];
        return response()->json($result);
    }

    # url,请求路径，postdata 请求body参数，注意是json格式
    public static function btnPost($url,$postdata)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json;charset=utf-8',
                'Content-Length: ' . strlen($postdata)
            )
        );
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }

    public static function getPost($url,$postdata,$header='')
    {


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json;charset=utf-8',
                'Content-Length: ' . strlen($postdata)
            )
        );

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json;charset=utf-8',
                'accesstoken:'.$header,
            )
        );
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;

    }

    public function checkAppToken()
    {
        $url = $this->kingdeeUrl."/ierp/api/getAppToken.do";
        $postdata = [
            "appId"=>"middleground",
            "appSecuret"=> "Kingdee@123456789",
            "accountId"=> "1404250515784597504"
        ];
        $app_token = Redis::get('app_token');
        if(!$app_token){
            $result = self::btnPost($url,json_encode($postdata));
            if($result){
                $res = json_decode($result);
                $app_token = $res->data->app_token;
                Redis::setex('app_token',7200,$app_token);
            }
        }
        return $app_token;
    }


    public function checkAccessToken()
    {
        $apptToken = $this->checkAppToken();
        $url = $this->kingdeeUrl."/ierp/api/login.do";
        $postdata = [
            "user"=>"18638659873",
            "apptoken"=>$apptToken,//换成上一步获取的token
            "accountId"=>"1404250515784597504"
        ];

        $accessToken = Redis::get('access_token');
        if(!$accessToken){
            $result = self::btnPost($url,json_encode($postdata));
            if($result){
                $res = json_decode($result);
                $accessToken = $res->data->access_token;
                Redis::setex('access_token',7200,$accessToken);
            }
        }
        return $accessToken;
    }


    public static function today()
    {
        list($y, $m, $d) = explode('-', date('Y-m-d'));
        return [
            date('Y-m-d H:i:s',mktime(0, 0, 0, $m, $d, $y)),
            date('Y-m-d H:i:s',mktime(23, 59, 59, $m, $d, $y))
        ];
    }
}
